//
//  RegistViewController.m
//  My - douban
//
//  Created by dllo on 15/11/1.
//  Copyright (c) 2015年 张鸿峰. All rights reserved.
//

#import "RegistViewController.h"

#import "registView.h"

#import "DataBaseSingleTon.h"

#import "User.h"


@interface RegistViewController ()

@property (nonatomic,retain) registView *userNameView;
@property (nonatomic,retain) registView *pwdView;
@property (nonatomic,retain) registView *pwdView2;
@property (nonatomic,retain) registView *emailView;
@property (nonatomic,retain) registView *phoneView;


@end

@implementation RegistViewController

- (void)dealloc
{
    [_userNameView release];
    [_pwdView release];
    [_pwdView2 release];
    [_emailView release];
    [_phoneView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"注册";
    
    
    self.view.backgroundColor = [UIColor brownColor];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registAction:)];
    
    
    self.userNameView = [[registView alloc] initWithFrame:CGRectMake(20, 80, 320, 40)];
    self.userNameView.leftLabel.text = @"用户名:";
    self.userNameView.rightTextField.placeholder = @"请输入用户名";
    [self.view addSubview:self.userNameView];
    [self.userNameView release];
    

    self.pwdView = [[registView alloc ]initWithFrame:CGRectMake(self.userNameView.frame.origin.x, self.userNameView.frame.origin.y + self.userNameView.frame.size.height + 20, self.userNameView.frame.size.width, self.userNameView.frame.size.height)];
    self.pwdView.leftLabel.text = @"密码:";
    self.pwdView.rightTextField.placeholder = @"请输入密码";
    [self.view addSubview:self.pwdView];
    [self.pwdView release];
    
    self.pwdView2 = [[registView alloc] initWithFrame:CGRectMake(self.userNameView.frame.origin.x, self.pwdView.frame.origin.y + self.pwdView.frame.size.height + 20, self.pwdView.frame.size.width, self.pwdView.frame.size.height)];
    self.pwdView2.leftLabel.text = @"确认密码:";
    self.pwdView2.rightTextField.placeholder = @"再次输入密码";
    [self.view addSubview:self.pwdView2];
    [self.pwdView2 release];
    
    self.emailView = [[registView alloc] initWithFrame:CGRectMake(self.userNameView.frame.origin.x, self.pwdView2.frame.origin.y + self.pwdView2.frame.size.height + 20, self.pwdView2.frame.size.width, self.pwdView2.frame.size.height)];
    self.emailView.leftLabel.text = @"邮箱:";
    self.emailView.rightTextField.placeholder = @"请输入邮箱";
    [self.view addSubview:self.emailView];
    [self.emailView release];
    
    self.phoneView = [[registView alloc] initWithFrame:CGRectMake(self.userNameView.frame.origin.x, self.emailView.frame.origin.y + self.emailView.frame.size.height + 20, self.emailView.frame.size.width, self.emailView.frame.size.height)];
    self.phoneView.leftLabel.text = @"联系方式:";
    self.phoneView.rightTextField.placeholder = @"请输入联系方式";
    [self.view addSubview:self.phoneView];
    [self.phoneView release];
    
    [[DataBaseSingleTon shareDataBase] createUserTable];
    

    
}

- (void)UIAlertController:(NSString *)str
{
    UIAlertController *alerC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alerC animated:YES completion:^{
        
        
    }];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)registAction:(UIBarButtonItem *)registBar
{
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM user"];
    NSMutableArray *arrM = [[DataBaseSingleTon shareDataBase] selectUserSQL:sql];
    
    User *user = [[User alloc] init];
    user.userName = self.userNameView.rightTextField.text;
    user.passWord = self.pwdView.rightTextField.text;
    
    BOOL result1 = [arrM containsObject:user.userName];
    
    NSLog(@"result1 ===== %d",result1);
    
    if (!result1) {
        
        [[DataBaseSingleTon shareDataBase] insertUser:user];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self UIAlertController:@"该用户已注册过"];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
